/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.yy.liuxue.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.gwc.yy.liuxue.entity.Requirement;
import com.gwc.yy.liuxue.service.RequirementService;

/**
 * 申请材料清单Controller
 * @author 黎俊涛
 * @version 2017-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/liuxue/requirement")
public class RequirementController extends BaseController {

	@Autowired
	private RequirementService requirementService;
	
	@ModelAttribute
	public Requirement get(@RequestParam(required=false) String id) {
		Requirement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = requirementService.get(id);
		}
		if (entity == null){
			entity = new Requirement();
		}
		return entity;
	}
	
	@RequiresPermissions("liuxue:requirement:view")
	@RequestMapping(value = {"list", ""})
	public String list(Requirement requirement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Requirement> page = requirementService.findPage(new Page<Requirement>(request, response), requirement); 
		model.addAttribute("page", page);
		return "yy/liuxue/requirementList";
	}

	@RequiresPermissions("liuxue:requirement:view")
	@RequestMapping(value = "form")
	public String form(Requirement requirement, Model model) {
		model.addAttribute("requirement", requirement);
		return "yy/liuxue/requirementForm";
	}

	@RequiresPermissions("liuxue:requirement:edit")
	@RequestMapping(value = "save")
	public String save(Requirement requirement, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, requirement)){
			return form(requirement, model);
		}
		requirementService.save(requirement);
		addMessage(redirectAttributes, "保存申请材料清单成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/requirement/?repage";
	}
	
	@RequiresPermissions("liuxue:requirement:edit")
	@RequestMapping(value = "delete")
	public String delete(Requirement requirement, RedirectAttributes redirectAttributes) {
		requirementService.delete(requirement);
		addMessage(redirectAttributes, "删除申请材料清单成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/requirement/?repage";
	}

}